package com.efresh.nico.gr_superman;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;

import java.util.List;


/**
 * Created by admin on 2015/11/19.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener{
    private Button login;
    private EditText username ;
    private EditText password ;
    private ProgressBar progressBar ;
    private ProgressDialog progressDialog ;

    //实现记住密码功能
    private SharedPreferences preferences ;
    private SharedPreferences.Editor editor ;
    private CheckBox remember_pass ;
    private CheckBox auto_login ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        remember_pass = (CheckBox) findViewById(R.id.remember_password) ;
        boolean isRemember = preferences.getBoolean("remember_pass",false);
        login = (Button) findViewById(R.id.login);
        auto_login = (CheckBox) findViewById(R.id.auto_login) ;
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        boolean autoLogin = preferences.getBoolean("autoLogin",false);

        progressBar = (ProgressBar) findViewById(R.id.progress);
        progressDialog = new ProgressDialog(LoginActivity.this);
        //实现自动登录功能
        if(autoLogin){
            progressDialog.setTitle("提示");
            progressDialog.setMessage("正在登录中....");
            progressDialog.setCancelable(false);
            progressDialog.show();
            String account = preferences.getString("username","") ;
            String pass = preferences.getString("password","");
            getUserInfo(account, pass);
        }else{
            if(isRemember){
                String account = preferences.getString("username","") ;
                String pass = preferences.getString("password","");
                username.setText(account);
                password.setText(pass);
                remember_pass.setChecked(true);
            }
            login.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.username:
                break;
            case R.id.password:
                break;
            case R.id.login:

                if(username.getText().toString().isEmpty()){
                    Toast.makeText(LoginActivity.this,"帐号不能为空",Toast.LENGTH_SHORT).show();
                }else if(password.getText().toString().isEmpty()){
                    Toast.makeText(LoginActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
                }else{
//                    progressBar.setVisibility(View.VISIBLE);
                    progressDialog.setTitle("提示");
                    progressDialog.setMessage("正在登录中....");
                    progressDialog.setCancelable(false);
                    progressDialog.show();
                    editor = preferences.edit();
                    if(remember_pass.isChecked()||auto_login.isChecked()){
                        editor.putBoolean("remember_pass",remember_pass.isChecked()); //记住密码
                        editor.putBoolean("autoLogin",auto_login.isChecked());   //自动登录
                        editor.putString("username", username.getText().toString());
                        editor.putString("password",password.getText().toString());
                    }else{
                        editor.clear();
                    }
                    editor.commit();
                    getUserInfo(username.getText().toString(), password.getText().toString());
                }
                break;
            default:
                break;
        }
    }

    public void getUserInfo(String username,String password){
        AVQuery<AVObject> query = new AVQuery<>("Courier");
        query.whereEqualTo("jobnumber",username);
        query.whereEqualTo("pwd",password);
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
               if(list.size()>0){
                    /*Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);*/
                   Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                   startActivity(intent);
//                   progressBar.setVisibility(View.GONE);
                   progressDialog.dismiss();
                   Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                   finish();
                }else{
                   progressDialog.dismiss();
                   Toast.makeText(LoginActivity.this,"帐号或者密码不正确", Toast.LENGTH_SHORT).show();

                }
            }
        });
    }
}
